package com.dianrui.fmi.activity;

import android.content.Context;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.dianrui.fmi.R;
import com.dianrui.fmi.event.RfreshAccount;
import com.dianrui.fmi.util.OkHttpUtils;
import com.dianrui.fmi.util.StringUtils;
import com.dianrui.fmi.util.ToastUtil;
import com.dianrui.fmi.util.Url;
import com.google.gson.JsonObject;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;

public class WithDrawActivity extends BaseActivity {
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.available_balance)
    TextView availableBalance;
    @BindView(R.id.withdraw_edit)
    EditText withdrawEdit;


    @Override
    public int getContentViewId() {
        return R.layout.activity_withdraw;
    }

    @Override
    public void initView() {
        title.setText(getString(R.string.withdraw));
        getBalanaceInfo();
    }



    @OnClick({R.id.back,R.id.withdraw})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;

            case R.id.withdraw:
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
                }
                final String money = withdrawEdit.getText().toString().trim();
                if (StringUtils.isEmpty(money)) {
                    ToastUtil.showToast(getString(R.string.please_input_withdraw_amount));
                    //提现金额不能大于可用金额
                } else if (StringUtils.toFloat(money) > StringUtils.toFloat(availableBalance.getText().toString())) {
                    ToastUtil.showToast(getString(R.string.withdraw_greater));
                } else {
                    startWithDraw();
                }
                break;

                default:
                    break;
        }
    }

    private void getBalanaceInfo() {
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("is_real", "1");
        OkHttpUtils.getInstance().postWithMemberToken(Url.GETBALACE, "",  new OkHttpUtils.XCallBack() {
            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result)) {
                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        if (null != jsonObject && !isFinishing()) {
                            if ("1000".equals(jsonObject.optString("status"))) {
                                availableBalance.setText(jsonObject.optJSONObject("data").optString("balance"));
                            } else {
                                ToastUtil.showToast(jsonObject.optString("message"));
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {

                    ToastUtil.showToast(getString(R.string.net_error));
                }
            }
        });


    }

    private void startWithDraw() {
        JsonObject jsonObject=new JsonObject();
        jsonObject.addProperty("money",withdrawEdit.getText().toString().trim());
        OkHttpUtils.getInstance().postWithMemberToken(Url.WITHDRAW, jsonObject.toString(), new OkHttpUtils.XCallBack() {
            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result)) {
                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        if (null != jsonObject && !isFinishing()) {
                            if ("1000".equals(jsonObject.optString("status"))) {
                                EventBus.getDefault().post(new RfreshAccount());
                                ToastUtil.showToast(jsonObject.optString("message"));
                                finish();
                            } else {
                                ToastUtil.showToast(jsonObject.optString("message"));
                            }

                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    ToastUtil.showToast(getString(R.string.net_error));
                }
            }
        });
    }
}
